package cn.itcast.bos.web.action.system;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.service.system.MenuService;
import cn.itcast.bos.web.action.common.BaseAction;

@Controller
@Scope("prototype")
@Namespace("/")//<package namespace extends
@ParentPackage("struts-default")
@Results({
	@Result(name="list", type="redirect", location="/pages/system/menu.jsp")
})  
public class MenuAction extends BaseAction<Menu> {
	
	@Autowired
	private MenuService menuService;
	
	
	/**
	  * @Description: 查询所有菜单数据
	  * @return json数组，通过children指定子菜单
	 */
	@Action("menuAction_findAll")
	public String findAll() throws Exception {
		List<Menu> list = menuService.findAll();
		//roles:noSession     parentMenu:死循环       childrenMenus:没用的数据
		this.java2Json(list, new String[]{"roles","parentMenu","childrenMenus"});
		return NONE;
	}
	
	
	
	/**
	 * 菜单保存(顶级菜单,子菜单)
	 */
	@Action("menuAction_save")
	public String save() throws Exception {
		menuService.save(model);
		return "list";
	}
	
	
	/**
	  * @Description: 查询所有的菜单数据
	  * @return  ztree的简单数据形式
		  标准：List<Menu> 长度：1
		 [
		 	{id:1,name:基础数据,children:[{id:2,name:收派标准}]}
		 ] 
		 简单：List<Menu> 长度：2
		 [
		 	{id:1,name:基础数据,pId:0},{id:2,name:收派标准,pId:1}
		 ]
	  *	  
	 */
	@Action("menuAction_findAllBySimple")
	public String findAllBySimple() throws Exception {
		List<Menu> list = menuService.findAllBySimple();
		this.java2Json(list, new String[]{"roles","childrenMenus","parentMenu","children"});
		return NONE;
	}
	
	
	
	/**
	  * @Description: 根据用户查询菜单-动态展示菜单数据
	  * @return  ztree的简单数据形式
		  标准：List<Menu> 长度：1
		 [
		 	{id:1,name:基础数据,children:[{id:2,name:收派标准}]}
		 ] 
		 简单：List<Menu> 长度：2
		 [
		 	{id:1,name:基础数据,pId:0},{id:2,name:收派标准,pId:1}
		 ]
	  *	  
	 */
	@Action("menuAction_showMenu")
	public String showMenu() throws Exception {
		List<Menu> list = menuService.showMenu();
		this.java2Json(list, new String[]{"roles","childrenMenus","parentMenu","children"});
		return NONE;
	}
	
}
